package sliver.action.shop;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.entity.Category;
import sliver.entity.Shopper;
import sliver.service.ShopManager;

import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.toListCategoriesAction")
public class ToListCategoriesAction extends ActionSupport implements RequestAware,SessionAware{

	private static final long serialVersionUID = -5198034345996864902L;
	
	private Map<String, Object> request;
	private Map<String, Object> session;
	
	@Autowired
	private ShopManager shopManager;
	
	public ToListCategoriesAction(){
		
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		
		Shopper shopper = Shopper.class.cast(session.get("login_shopper"));
		
		List<List<Category>> categories = new ArrayList<List<Category>>();
		
		List<Category> roots = shopManager.obtainShopRootCategory(shopper);
		for(Category root : roots){
			List<Category> subs = shopManager.obtainShopCategory(shopper.getId(), root.getId());
			
			categories.add(subs);
		}
		
		request.put("categories", categories);
		
		return SUCCESS;
	}
	
}
